using WeatherSystem.API.Models;

namespace WeatherSystem.API.Specifications
{
    /// <summary>
    /// 用户查询规范
    ///
    /// 提供多种用户查询方式，支持按搜索关键词、角色、活跃状态等条件筛选
    /// 所有查询默认按用户名升序排序
    /// </summary>
    public class UserSpecification : BaseSpecification<User>
    {
        /// <summary>
        /// 综合查询构造函数
        /// </summary>
        /// <param name="search">搜索关键词（匹配用户名或邮箱）</param>
        /// <param name="role">用户角色过滤</param>
        /// <param name="isActive">活跃状态过滤</param>
        public UserSpecification(string? search = null, UserRole? role = null, bool? isActive = null)
            : base(u =>
                (string.IsNullOrEmpty(search) || u.Username.Contains(search) || u.Email.Contains(search)) &&
                (!role.HasValue || u.Role == role.Value) &&
                (!isActive.HasValue || u.IsActive == isActive.Value))
        {
            // 按用户名升序排列
            ApplyOrderBy(u => u.Username);
        }

        /// <summary>
        /// 根据ID查询用户
        /// </summary>
        /// <param name="id">用户ID</param>
        public UserSpecification(Guid id)
            : base(u => u.Id == id)
        {
        }

        /// <summary>
        /// 根据用户名查询用户
        /// </summary>
        /// <param name="username">用户名</param>
        public UserSpecification(string username)
            : base(u => u.Username == username)
        {
        }

        /// <summary>
        /// 根据邮箱查询用户
        /// </summary>
        /// <param name="email">邮箱地址</param>
        /// <param name="isEmail">参数标识（用于重载区分）</param>
        public UserSpecification(string email, bool isEmail)
            : base(u => u.Email == email)
        {
        }

        /// <summary>
        /// 自定义谓词查询构造函数
        /// </summary>
        /// <param name="predicate">自定义查询条件</param>
        public UserSpecification(System.Linq.Expressions.Expression<Func<User, bool>> predicate)
            : base(predicate)
        {
        }
    }
}